require('./PageCopy.styl');
import { Control } from 'react-keeper'
const reactMixin = require('react-mixin');
const Actions = require('./actions');
const Store = require('./store');
const PendingList = require('components/pendinglist')
const Search = require('components/search')
const WithRefresh = require('components/withrefresh');

const ListWithCopy = WithRefresh(
    PendingList,
    (pageNum, props, success, error) => {
        Actions.fetch(pageNum, props, success, error)
    }
);

class Copy extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            keyword: "",
            currentType: 0 //0 搜索项目名称， 1 搜索项目编号
        };
        this.searchRange = ["项目名称","项目编号"]
        this.handleChangeSearchType = this.handleChangeSearchType.bind(this);
    }
    onsearch(keyword) {
        this.setState({
            keyword:keyword
        })
        if (keyword != undefined) {
            var props = {
                pageSize: 10,
            }
            if (!!this.state.currentType) {//全为数字的是项目编号
                props.proj_num = keyword
                Actions.fetch(0, props)
            } else {
                props.proj_name = keyword
                Actions.fetch(0, props)
            }
        }
    }
    onexit(){//退出搜索
        this.setState({
            keyword:"",
            listData:[]
        })
    }
    
    handleChangeSearchType(currentType) {
        this.setState({ currentType })
    }

    render() {
        let t = this;
        if (Control.state)
            var origin = Control.state.origin;
        else
            var origin = "";
        return (
            <div className="copy">
                <Search onsearch={this.onsearch.bind(this)} searchRange={this.searchRange} data={this.state.listData}
                    origin={origin} onExit={this.onexit.bind(this)} currentType={t.state.currentType} handleChangeSearchType={this.handleChangeSearchType}/>
                <ListWithCopy className="msg" origin={origin} toolbar={false}/>
            </div>
        );
    }

    componentWillMount() {
    }

    componentDidMount() {
    }

    componentWillReceiveProps(nextProps) {
    }

    shouldComponentUpdate(nextProps, nextState) {
        return true;
    }

    componentWillUpdate(nextProps, nextState) {
    }

    componentDidUpdate(prevProps, prevState) {
    }

    componentWillUnmount() {
    }
}

reactMixin.onClass(Copy, Reflux.connect(Store));

module.exports = Copy;
